<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
  <el-form-item label="员工" prop="partyId">
    <el-select v-model="queryParams.partyId" placeholder="请选择员工" clearable filterable>
      <el-option
          v-for="dict in partyIdOptions"
          :key="dict.partyId"
          :label="dict.firstName"
          :value="dict.partyId"
      />
    </el-select>
  </el-form-item>
  <el-form-item label="补签状态" prop="signStatusId">
    <el-select v-model="queryParams.signStatusId" placeholder="请选择补签状态" clearable>
      <el-option
          v-for="dict in signStatusIdOptions"
          :key="dict.enumCode"
          :label="dict.description"
          :value="dict.enumCode"
      />
    </el-select>
  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
        >
          <router-link :to="'/workflow/approve/sign_agin/apply'" class="link-type">
              <span> 补签申请</span>
          </router-link>
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ehr:sign_agin:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sign_aginList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="补签记录" align="center" prop="signRecordId" />
      <el-table-column label="员工" align="center" prop="partyId"  :formatter="partyIdFormat"/>
      <el-table-column label="原因描述" align="center" prop="otherContent" />
      <el-table-column label="补签状态" align="center" prop="signStatusId"  :formatter="signStatusIdFormat"/>
      <el-table-column label="补签次数" align="center" prop="attendanceTimes" />
  <el-table-column label="更新时间" align="center" prop="lastUpdatedStamp" width="180">
    <template slot-scope="scope">
      <span>{{ parseTime(scope.row.lastUpdatedStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
    </template>
  </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['ehr:sign_agin:detail']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改 补签对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled = detailFlag>
        <el-row>
            <el-col :span="12" >
                <el-form-item label="申请日期" prop="applyDate">
                    <span>{{ form.applyDate }}</span>
                </el-form-item>
            </el-col>
            <el-col :span="12" >
                <el-form-item label="员工" prop="partyId">
                    <span>{{ personFormat(form) }}</span>
                </el-form-item>
            </el-col>
        </el-row >
        <el-row>
            <el-col :span="12" >
                <el-form-item label="异常记录" prop="attendanceExceptionId">
                    <span>{{ attendanceExceptionIdFormat(form) }}</span>
                </el-form-item>
            </el-col>
        </el-row >
        <el-row>           
            <el-col :span="12" >
                <el-form-item label="上班时间" prop="workStartTime">
                    <span>{{ form.workStartTime }}</span>
                </el-form-item>
            </el-col>

            <el-col :span="12" >
                <el-form-item label="下班时间" prop="workEndTime">
                    <span>{{ form.workEndTime }}</span>
                </el-form-item>
            </el-col>
        </el-row >
        <el-row>            
            <el-col :span="12" >
                <el-form-item label="申请原因" prop="signCause">
                    <span>{{ signCauseFormat(form) }}</span>
                </el-form-item>
            </el-col>
        </el-row >
        <el-row>
            <el-col :span="20" >
                <el-form-item label="原因描述" prop="otherContent">
                    <span>{{ form.otherContent }}</span>
                </el-form-item>
            </el-col>
        </el-row >
        <el-row>           
            <el-col :span="12" >
                <el-form-item label="审批人" prop="approver">
                    <span>{{ approverFormat(form) }}</span>
                </el-form-item>
            </el-col>
        </el-row >
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSign_agin, getSign_agin, delSign_agin, addSign_agin, updateSign_agin } from "@/api/ehr/sign_agin";
// person enum api
import { getPersonEnum} from "@/api/ehr/person";
export default {
  name: "Sign_agin",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //  补签表格数据
      sign_aginList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 人员字典
      personOptions: [],
      // 员工业务字典
      partyIdOptions: [],
      // 异常记录业务字典
      attendanceExceptionIdOptions: [],
      // 申请原因业务字典
      signCauseOptions: [],
      // 补签状态业务字典
      signStatusIdOptions: [],
      // 补签类型:M：上午，A：下午,ALL:全天业务字典
      attendanceTypeOptions: [],
      // 流程状态业务字典
      flowStatusOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc:"", // 升序或降序 大写的 ASC或DESC,为空代表升序
        partyId: null,
        signStatusId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        attendanceExceptionId: [
          { required: true, message: "异常记录不能为空", trigger: "change" }
        ],
        approver: [
          { required: true, message: "审批人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
      // 人员字典
      getPersonEnum().then((response) => {
          this.personOptions = response.data;
      });
  // 员工业务字典
  this.getEntityDicts("person:partyId:firstName").then((response) => {
    this.partyIdOptions = response.data;
  });
  // 异常记录业务字典
  this.getEntityDicts("AttendanceDailyAccount:dailyAcountId:dailyAcountId").then((response) => {
    this.attendanceExceptionIdOptions = response.data;
  });
        // 申请原因业务字典
        this.getEnumDicts("SIGN_AGIN_REASON").then((response) => {
            this.signCauseOptions = response.data;
        });
        // 补签状态业务字典
        this.getEnumDicts("FLOW_STATUS").then((response) => {
            this.signStatusIdOptions = response.data;
        });
    // 流程状态业务字典
    this.getEnumDicts("FLOW_STATUS").then((response) => {
      this.flowStatusOptions = response.data;
    });
    this.getList();
  },
  methods: {
      // 人员字典转换
      personFormat(row, column) {
          let val = null;
          this.personOptions.forEach((item, index) => {
              if (row.partyId === item.partyId) {
              val = item.name;
          }
      });
          return val;
      },
      // 审批人字典转换
      approverFormat(row, column) {
          let val = null;
          this.personOptions.forEach((item, index) => {
              if (row.approver === item.partyId) {
              val = item.name;
          }
      });
          return val;
      },
     partyIdFormat(row, column) {
      let val = null;
      this.partyIdOptions.forEach((item, index) => {
        if (row.partyId === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
     attendanceExceptionIdFormat(row, column) {
      let val = null;
      this.attendanceExceptionIdOptions.forEach((item, index) => {
        if (row.attendanceExceptionId === item.dailyAcountId) {
          val = item.dailyAcountId;
        }
      });
      return val;
    },
     signCauseFormat(row, column) {
      let val = null;
      this.signCauseOptions.forEach((item, index) => {
        if (row.signCause === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
     signStatusIdFormat(row, column) {
      let val = null;
      this.signStatusIdOptions.forEach((item, index) => {
        if (row.signStatusId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
     flowStatusFormat(row, column) {
      let val = null;
      this.flowStatusOptions.forEach((item, index) => {
        if (row.flowStatus === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询 补签列表 */
    getList() {
      this.loading = true;
      listSign_agin(this.queryParams).then(response => {
        this.sign_aginList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        signRecordId: null,
        applyDate: null,
        partyId: null,
        attendanceExceptionId: null,
        workStartTime: null,
        workEndTime: null,
        signCause: null,
        otherContent: null,
        signSystemCause: null,
        signStatusId: null,
        attendanceRuleId: null,
        attendanceAddId: null,
        attendanceType: null,
        attendanceTimes: null,
        flowInstId: null,
        flowStatus: null,
        approver: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.signRecordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editFlag = true,
      this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加 补签";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
      this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      const signRecordId = row.signRecordId || this.ids
      getSign_agin(signRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = " 补签详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const signRecordId = row.signRecordId || this.ids
      getSign_agin(signRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改 补签";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.signRecordId != null && this.updateFlag) {
            updateSign_agin(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSign_agin(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const signRecordIds = row.signRecordId || this.ids;
      this.$modal.confirm('是否确认删除 补签编号为"' + signRecordIds + '"的数据项？').then(function() {
        return delSign_agin(signRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/sign_agin/export', {
        ...this.queryParams
      }, `sign_agin_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
